<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>面向对象</title>

    <style>
        #t {
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>

</head>

<body>

    <div id="t"></div>

</body>

<script>
    class Drag {
        constructor(id) {
            this.oDiv = document.getElementById(id);
        }

        FunDown() {
            this.oDiv.onmousedown = function(e) {

                var x = e.pageX - this.offsetLeft;
                var y = e.pageY - this.offsetTop;
                this.onmouseup = function() {
                    this.onmousemove = null;
                }
                this.onmousemove = function(e) {
                    this.style.left = (e.pageX - x) + 'px';
                    this.style.top = (e.pageY - y) + 'px';
                }

            }
        }


    }
    var s = new Drag('t');
    s.FunDown();


    /* var t = document.getElementById('t');
    t.onmousedown = function(e) {
        var x = e.pageX - t.offsetLeft;
        var y = e.pageY - t.offsetTop;
        t.onmousemove = function(e) {
            console.log(e.pageX);
            t.style.left = (e.pageX - x) + 'px';
            t.style.top = (e.pageY - y) + 'px';

        }
        t.onmouseup = function() {
            this.onmousemove = null;
        }
    } */
</script>

</html>